package ui;

import java.awt.BorderLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import Work.*;

/*书籍增删改*/
public class BookOther extends JFrame implements ActionListener {
	JTextField jtf1, jtf2, jtf3, jtf4, jtf5;
	private JButton updateb_name;
	private JButton updateauthor;
	private JButton updateprice;
	private JButton updateamount;
	private JButton returnbutton;

	// 创建数据库连接类对象
	Jdbc db = new Jdbc();

	// 添加书籍
	public void AddBook() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(4, 2, 5, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("添加书籍"));
		JLabel jl2 = new JLabel("书籍名称:");
		jtf2 = new JTextField(10);
		JLabel jl3 = new JLabel("书籍作者:");
		jtf3 = new JTextField(10);
		JLabel jl4 = new JLabel("书籍价格:");
		jtf4 = new JTextField(10);
		JLabel jl5 = new JLabel("库存数量:");
		jtf5 = new JTextField(10);
		jp1.add(jl2);
		jp1.add(jtf2);
		jp1.add(jl3);
		jp1.add(jtf3);
		jp1.add(jl4);
		jp1.add(jtf4);
		jp1.add(jl5);
		jp1.add(jtf5);
		jpc.add(jp1);// 把面板jp1加到流式布局上
		add(jpc);// 添加流式布局
		// add(jpc, BorderLayout.CENTER);

		JButton jb1 = new JButton("提交");
		jb1.addActionListener(this);
		jb1.setActionCommand("add");// 没看懂

		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);

		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("书籍添加界面");
		setSize(280, 215);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}

	// 删除书籍
	public void DeleteBook() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(3, 2, 5, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("删除书籍（任选其一输入）"));
		JLabel jl1 = new JLabel("书籍编号:");
		jtf1 = new JTextField(10);
		JLabel jl2 = new JLabel("书籍名称:");
		jtf2 = new JTextField(10);
		JLabel jl3 = new JLabel("书籍作者:");
		jtf3 = new JTextField(10);
		jp1.add(jl1);// 在面板jp1上添加标签jl1
		jp1.add(jtf1);// 在面板jp1上添加输入框
		jp1.add(jl2);
		jp1.add(jtf2);
		jp1.add(jl3);
		jp1.add(jtf3);
		jpc.add(jp1);// 把面板jp1加到流式布局上
		add(jpc);// 添加流式布局
		// add(jpc, BorderLayout.CENTER);

		JButton jb1 = new JButton("提交");
		jb1.addActionListener(this);
		jb1.setActionCommand("delete");// 没看懂

		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);

		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("书籍删除界面");
		setSize(280, 185);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}

	// 修改书籍
	public void UpdateBookButton() {
		JFrame jFrame = new JFrame("修改部分选择");
		// JPanel jpc = new JPanel();// 默认流式布局
		jFrame.setLayout(new BorderLayout());
		// jFrame.setSize(300, 200);
		JPanel jp1 = new JPanel(new GridLayout(2, 2, 10, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("书籍修改"));
		// this.setLayout(new FlowLayout());
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		setTitle("书籍修改系统功能选项");
		setSize(300, 200);
		setLocationRelativeTo(null);

		// 监听右上角的退出按钮
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent windowEvent) {
				System.exit(0);
			}
		});
		updateb_name = new JButton("修改书籍名称");
		updateauthor = new JButton("修改书籍作者");
		updateprice = new JButton("修改书籍价格");
		updateamount = new JButton("修改库存数量");

		jp1.add(updateb_name);
		jp1.add(updateauthor);
		jp1.add(updateprice);
		jp1.add(updateamount);
		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方
		getContentPane().add(jp1, BorderLayout.CENTER);
		// add(jpc);
		// add(jpc, BorderLayout.CENTER);
		// jp1.setBounds(200, 50, 150, 40);

		updateb_name.addActionListener(this);
		updateauthor.addActionListener(this);
		updateprice.addActionListener(this);
		updateamount.addActionListener(this);

		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		ChangeBook changebook = new ChangeBook();
		ArrayList<Book> booklist = changebook.read(db);
		ForeignKey foreignkey = new ForeignKey();
		BookOther bookother = new BookOther();
		BookOther1 bookother1 = new BookOther1();
		if (e.getSource() == updateb_name) {// 修改
			this.dispose();
			bookother1.Updateb_name();
		} else if (e.getSource() == updateauthor) {
			this.dispose();
			bookother1.Updateb_author();
		} else if (e.getSource() == updateprice) {
			this.dispose();
			bookother1.Updateprice();
		} else if (e.getSource() == updateamount) {
			this.dispose();
			bookother1.Updateamount();
		} else if (e.getActionCommand().equals("add")) {// 增加
			String s2 = jtf2.getText().trim();
			String s3 = jtf3.getText().trim();
			String s4 = jtf4.getText().trim();
			String s5 = jtf5.getText().trim();
			if (s2.equals("") || s3.equals("") || s4.equals("") || s5.equals("")) {
				JOptionPane.showMessageDialog(this, "内容缺失，请检查是否填写完毕全部信息");
				// !!!查重
			} else {
				db.uiinsertbook(db, s2, s3, s4, s5);
				JOptionPane.showMessageDialog(this,
						"书籍名称:" + s2 + "\n书籍作者:" + s3 + "\n书籍价格:" + s4 + "\n库存数量:" + s5 + "\n书籍信息添加成功");
				this.dispose();
				bookother.AddBook();
			}
		} else if (e.getActionCommand().equals("delete")) {// 删除
			String s1 = jtf1.getText().trim();
			String s2 = jtf2.getText().trim();
			String s3 = jtf3.getText().trim();
			if (s1.equals("") && s2.equals("")) {
				if (!foreignkey.FindB_AUTHOR(booklist, s3)) {
					JOptionPane.showMessageDialog(this, "查无此书，请重新输入！");
				} else {
					db.uideletebook(db, s3, 3);
					JOptionPane.showMessageDialog(this, "书籍作者为 " + s3 + " 的书籍信息已删除");
					this.dispose();
					bookother.DeleteBook();
				}
			} else if (s1.equals("") && s3.equals("")) {
				if (!foreignkey.FindB_NAME(booklist, s2)) {
					JOptionPane.showMessageDialog(this, "查无此书，请重新输入！");
				} else {
					db.uideletebook(db, s2, 2);
					JOptionPane.showMessageDialog(this, "书籍名称为 " + s2 + " 的书籍信息已删除");
					this.dispose();
					bookother.DeleteBook();
				}
			} else if (s2.equals("") && s3.equals("")) {
				if (!foreignkey.FindB_ID(booklist, s1)) {
					System.out.println("1");
					JOptionPane.showMessageDialog(this, "查无此书，请重新输入！");
				} else {
					db.uideletebook(db, s1, 1);
					JOptionPane.showMessageDialog(this, "书籍编号为 " + s1 + " 的书籍信息已删除");
					this.dispose();
					bookother.DeleteBook();
				}
			}
		} else if (e.getSource() == returnbutton) {
			this.dispose();
			new LibrarianOp();
		} else {
			jtf1.setText("");
			jtf2.setText("");
			jtf3.setText("");
			jtf4.setText("");
			jtf5.setText("");
		}

	}
}
